package com.everflourish.yeah100.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

/**
 * 通知工具类
 *
 * @author pf
 * @date 2015-4-13
 */
public class NotificationUtil {

    private Context mContext;
    private NotificationManager mNm;
    private NotificationCompat.Builder mBuilder;
    private SPUtil spUtil;

    public NotificationUtil(Context context) {
        mFlagId++;
        mContext = context;
        mNm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle("测试标题")
                .setContentText("测试内容")
                .setContentIntent(
                        getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
                        // .setNumber(number)//显示数量
                .setTicker("测试通知来啦")// 通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())// 通知产生的时间，会在通知信息里显示
                .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级,API 16以上
                .setAutoCancel(true)// 设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)// ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_SOUND)// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
                        // Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 //
                        // requires VIBRATE permission
                .setSmallIcon(context.getApplicationInfo().icon);
    }

    private static int mFlagId = 0;
    private static boolean isSingleNotify = true;// 是否是单个通知，只显示一个通知，默认是true

    public void setIsSingLeNotifyNo(boolean isSingleNotify) {
        NotificationUtil.isSingleNotify = isSingleNotify;
    }

    /**
     * 显示一个通知
     *
     * @param setTickerTitle 通知栏首次出现的标题，带上升动画效果的
     * @param title          通知的标题
     * @param content        通知的内
     * @return
     */
    public int showNotification(String setTickerTitle, String title,
                                String content) {
        mBuilder.setTicker(setTickerTitle);
        return showNotification(title, content);
    }

    /**
     * @param setTickerTitle
     * @param title
     * @param content
     * @return
     */
    public int showNotification(String setTickerTitle, String title,
                                String content, PendingIntent intent) {
        mBuilder.setTicker(setTickerTitle);
        if (intent != null) {
            mBuilder.setContentIntent(intent);
        }
        return showNotification(title, content);
    }

    /**
     * 显示一个通知
     *
     * @param title   通知的标题
     * @param content 通知的内容
     */
    public int showNotification(String title, String content) {
        if (!isSingleNotify) {
            mFlagId++;
        }
        mBuilder.setContentTitle(title).setContentText(content);
        int isSoundOrVibrate = 0;
        spUtil = SPUtil.getSharedPreferences(mContext);
        if (!spUtil.getBoolean(SPUtil.MUTE, true)) {
            isSoundOrVibrate = Notification.DEFAULT_SOUND | isSoundOrVibrate;
        }
        if (!spUtil.getBoolean(SPUtil.VIBRATION, true)) {
            isSoundOrVibrate = Notification.DEFAULT_VIBRATE | isSoundOrVibrate;
        }
        mBuilder.setDefaults(isSoundOrVibrate);
        mNm.notify(mFlagId, mBuilder.build());
        return mFlagId;
    }

    /**
     * 显示进度的通知
     *
     * @param max             进度条的最大值
     * @param currentProgress 进度条当前的进度
     * @param setTickerTitle  通知栏出现通知（最上面）的标题
     * @param title           通知栏的标题
     * @param content         通知栏的内容
     * @param intent          点击事件
     */
    public int showProgressNotify(int max, int currentProgress,
                                  String setTickerTitle, String title, String content,
                                  PendingIntent intent) {
        if (max <= currentProgress) {// 下载完毕
            mNm.cancel(mFlagId);
            mBuilder.setContentTitle(title).setContentText(content)
                    .setTicker(setTickerTitle).setAutoCancel(true)
                    .setOngoing(false).setProgress(max, currentProgress, false);
            if (intent != null) {
                mBuilder.setContentIntent(intent);
            }
            int isSoundOrVibrate = 0;
            spUtil = SPUtil.getSharedPreferences(mContext);
            if (!spUtil.getBoolean(SPUtil.MUTE, true)) {
                isSoundOrVibrate = Notification.DEFAULT_SOUND
                        | isSoundOrVibrate;
            }
            if (spUtil.getBoolean(SPUtil.VIBRATION, true)) {
                isSoundOrVibrate = Notification.DEFAULT_VIBRATE
                        | isSoundOrVibrate;
            }
            mBuilder.setDefaults(isSoundOrVibrate);
        } else {// 正在下载
            mBuilder.setContentTitle(title).setContentText(content)
                    .setTicker(setTickerTitle).setAutoCancel(false)
                    .setOngoing(true).setDefaults(0)
                    .setProgress(max, currentProgress, false);
            if (intent != null) {
                mBuilder.setContentIntent(intent);
            }
        }
        mNm.notify(mFlagId, mBuilder.build());
        return mFlagId;
    }

    /**
     * 取消通知
     *
     * @param flagId
     */
    public void cancelNotify(int flagId) {
        mNm.cancel(flagId);
    }

    /**
     * @获取默认的pendingIntent,为了防止2.3及以下版本报错
     * @flags属性: 在顶部常驻:Notification.FLAG_ONGOING_EVENT 点击去除：
     * Notification.FLAG_AUTO_CANCEL
     */
    private PendingIntent getDefalutIntent(int flags) {
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1,
                new Intent(), flags);
        return pendingIntent;
    }
}
